<?
ob_start(); 
include("common/header.html");
include("common/left.html");
include("common/page.php");
include_once("/var/www/html/new/d/class/SimpleExcel.class.php");
$out_html=ob_get_contents();
ob_end_clean();
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$currentPage    = $_GET["currentPage"] ? $_GET["currentPage"]:1;
$pageSize       = $_GET['pageSize']    ? $_GET["pageSize"]:10;
if($_REQUEST['act']=='loadExcel'){
    $currentPage = 1;
    $pageSize = 9999;
}
$txt   =$_REQUEST['txt'];
$dtype = intval($_GET['dtype']);
if(!in_array($dtype,array(0,1,7)))
    exit('Access Denied');
$ctype=$_REQUEST['com_type'];
$prov=$_REQUEST['d_province']?$_REQUEST['d_province']:0;
$city=$_REQUEST['d_city']?$_REQUEST['d_city']:0;
$mb=$tpl=$b=$a=$a_sort=array();
// if(!$dtype){
	// $sel  ="select dtype from pft_member where id=".$_SESSION['sid']." limit 1";
	// $GLOBALS['le']->query($sel);
	// $GLOBALS['le']->fetch_assoc();
	// $dtype = $GLOBALS['le']->f("dtype");
// }
if($_SESSION['group']){
	$sel="select son_id from pft_member_relationship where status=0 and ship_type=1 and parent_id=".$_SESSION['group'];
	$GLOBALS['le']->query($sel);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$mb[]=$row['son_id'];
	}
}
if($txt && $txt!="请输入企业名称\分销账号\手机"){//模糊查询左边的内容
	$tj=" and (instr(dname,'".$txt."') or instr(cname,'".$txt."') or account='".$txt."' or mobile='".$txt."')";
}
if($prov || $city || $ctype){//省份 城市 企业类型
	$a_t=" left join pft_member_extinfo e on e.fid=m.id ";
	$a_tj.=$prov?" and province=".$prov:"";
	$a_tj.=$city?" and city=".$city:"";
	if($ctype){
		$a_tj.=" and (";
		foreach($ctype as $v){
			$a_tj.="instr(com_type,'".$v."') or ";
		}
		$a_tj=trim($a_tj,"or ").")";
	}
}
if($dtype==1){
	$ltitle="供应商列表";
	$sel1="select dname,cname,account,mobile,m.id,ship_type ";
	$sel2="select count(*) total ";
	$sel="from pft_member_relationship r left join pft_member m on m.id=parent_id ".$a_t." where m.status in (0,3) ".$tj.$a_tj." and r.status=0 and son_id=".$_SESSION['sid']." and ship_type=0 and parent_id!={$_SESSION['sid']}";
}
elseif($dtype==7){
	$_dtype=isset($_REQUEST['_dtype'])?$_REQUEST['_dtype']:"0,3";
	$ltitle="集团成员列表";
	$sel1="select dname,cname,account,mobile,m.id,ship_type,son_id_type ";
	$sel2="select count(*) total ";
	$sel="from pft_member_relationship r left join pft_member m on m.id=son_id ".$a_t." where m.status in (0,3) ".$tj.$a_tj." and r.status=0 and parent_id=".$_SESSION['sid']." and son_id_type in (".$_dtype.") and ship_type=1";
}
else if($dtype==0){
	$ltitle="分销商管理";
	$sel="select id,title from uu_fx_pri_tpl where distid=".$_SESSION['sid'];
	$GLOBALS['le']->query($sel);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$tpl[]=$row;        
	}
    foreach($tpl as $v){
        $tpls[] = $v['id'];
    }
	$sel1="select dname,cname,account,mobile,m.id,son_id_type,ship_type ";
	$sel2="select count(*) total ";
	$sel="from pft_member_relationship r left join pft_member m on m.id=son_id ".$a_t." where m.status in (0,3) ".$tj.$a_tj." and r.status=0 and parent_id=".$_SESSION['sid']." and ship_type=0 and length(m.account)=6 and son_id!={$_SESSION['sid']}";
	if(isset($_REQUEST['intel'])) $sel="from pft_member_relationship r left join pft_member m on m.id=son_id ".$a_t." where m.status in (0,3) ".$tj.$a_tj." and r.status=0 and parent_id=".$_SESSION['sid']." and ship_type=0 and son_id!={$_SESSION['sid']}";
    //三亚先行账户因招同行投诉分销商只显示800个活跃用户 By cgp @ 2015年8月17日11:16:47
    if ($_SESSION['memberID']==55) {
//        $sell .= " AND son_id BETWEEN 7593 AND 13626";
    }
}
$GLOBALS['le']->query($sel1.$sel." limit ".($currentPage-1)*$pageSize.",".$pageSize);
while($row=$GLOBALS['le']->fetch_assoc()){
    // if(ismobile($row['account']))
        // continue;
 if($maccount!=$row['account'])
	{switch($row['son_id_type']){
		case 0:$row['s_type']="分销商";break;
		case 1:$row['s_type']="资源方";break;
	}
	if($dtype==1){$row['s_type']="供应商";}
	switch($row['ship_type']){
		case 0:$row['type']="供销关系";break;
		case 1:$row['type']="从属关系";break;
		case 2:$row['type']="平级关系";break;
		case 3:$row['type']="推荐关系";break;
	}
	$a_sort[$row['id']]=$row;
  } 
 }
if ($_SESSION['memberID']==55) {
    $total = 802;
} else {
    $GLOBALS['le']->query($sel2.$sel);
    $GLOBALS['le']->fetch_assoc();
    $total=$GLOBALS['le']->f("total");
}

// $total = sizeof($a_sort);
$parid=array_keys($a_sort);
if($dtype==0){
	if($parid){
		$sel="select fxid,tplid from uu_fx_tpl_cs where fxid in (".implode(",",$parid).")";
		$GLOBALS['le']->query($sel);
		while($row=$GLOBALS['le']->fetch_assoc()){
            if(is_array($tpls) && in_array($row['tplid'],$tpls))
			$_tpl[$row['fxid']]=$row['tplid'];
		}
		$_a=simplexml_load_string($s->PFT_Member_Fund(implode(",",$parid),1,$_SESSION['sid']));
		foreach($_a as $v){
			$a[(int)$v->UUfid]=$v->UUkmoney/100;
		}
		$_b=simplexml_load_string($s->PFT_Member_Fund(implode(",",$parid),2,$_SESSION['sid']));
		foreach($_b as $v){
			$b[(int)$v->UUfid]=$v->UUbasecredit/100;
		}
	}
}
else if($dtype==7){
	$parid=array();
	$GLOBALS['le']->query("select son_id ".$sel);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$parid[]=$row['son_id'];
	}
	if($parid){
		$_a=simplexml_load_string($s->PFT_Member_Fund(implode(",",$parid),0));
		foreach($_a as $v){
			$count+=$a[(int)$v->UUfid]=$v->UUamoney/100;
		}
	}
}
else if($dtype==1){
    $meng = $shen = array();//盟 审
    $shenT = array(
        '0'=>'正常',
        '1'=>'退出',
        '2'=>'已申请',
        '3'=>'已驳回'
    );
	foreach($parid as $v){
		$_a=simplexml_load_string($s->PFT_Member_Fund($_SESSION['sid'],1,$v));
		$a[$v]=$_a->Rec[0]->UUkmoney/100;
		$_b=simplexml_load_string($s->PFT_Member_Fund($_SESSION['sid'],2,$v));
		$b[$v]=$_b->Rec[0]->UUbasecredit/100;
        //查询联盟内的相关信息 zax -2015.05.28

        $sql = "select id from pft_distributor_union_SE where fid=$v limit 1";
        $GLOBALS['le']->query($sql);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $meng[$v] =1;

        }
	}
    //是盟主的情况下 再去查询本人在这个联盟内的状态
    foreach($meng as $ke => $va){
        $sql = "select dstatus from pft_union_member_info_SE where fid=$ke and
                    memberID={$_SESSION['sid']} limit 1";
        $GLOBALS['le']->query($sql);
        if($row=$GLOBALS['le']->fetch_assoc()){
            $shen[$ke] = $shenT[$row['dstatus']];
        }else{
            $shen[$ke] = '尚未加入该联盟';
        }
    }
}

$txt=$txt?$txt:"请输入企业名称\分销账号\手机";
if($_GET['cs']){
    print_r($meng);
    print_r($shen);
}
if($_REQUEST['act']=='loadExcel'){
    $filename=date('YmdHis').'集团成员列表';
	$Excel[0]['a']='企业名称（账号）';
	$Excel[0]['b']='联系人';
	$Excel[0]['c']='手机号码';
	$Excel[0]['d']='资金金额';
	$i=1;
	foreach($a_sort as $data){
		$n++;
		$Excel[$n]['a']=$data['dname'].'('.$data['account'].')';
		$Excel[$n]['b']=$data['cname'];
		$Excel[$n]['c']=$data['mobile'];
		$Excel[$n]['d']=(float)$a[$data['id']];
	}
	$xls = new SimpleExcel('UTF-8', true,'ml');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
	exit;
}
echo $out_html;
if ($_GET['dev']) {
    include("tpl/partner_dev.html");
} else {
    include("tpl/partner.html");

}
include("common/footer.html");
